package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.commonutils.ResultCode;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 课程表excel的读取监听器
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    private EduSubjectService eduSubjectService;

    // 因为Listener在servlet容器中，不在spring容器中，所以不能注入，需要自己手动new
    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    /**
     * 逐行读取excel内容
     *
     * @param subjectData     excel的一行
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        //表示excel中没有数据，就不需要读取了
        if (subjectData == null) {
            throw new GuliException(ResultCode.ERROR, "excel读取失败");
        }

        //一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类
        EduSubject existOneSubject = this.existOneSubject(subjectData.getOneSubjectName());
        //判断是否有一级分类是否重复
        if (existOneSubject == null) {
            //没有相同的一级分类，进行添加
            existOneSubject = new EduSubject();
            //设置一级分类id值，0代表为一级分类
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getOneSubjectName());
            eduSubjectService.save(existOneSubject);
        }

        //获取一级分类的id值
        String parentId = existOneSubject.getId();
        //判断是否有二级分类是否重复
        EduSubject existTwoSubject = this.existTwoSubject(subjectData.getTwoSubjectName(), parentId);
        if (existTwoSubject == null) {
            //没有相同的二级分类，进行添加
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(parentId);
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());
            eduSubjectService.save(existTwoSubject);
        }
    }

    /**
     * 判断一级分类是否存在
     *
     * @param name              分类名称
     * @return null不存在，非null存在
     */
    private EduSubject existOneSubject(String name) {
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<EduSubject>()
                .eq(EduSubject::getParentId, "0")
                .eq(EduSubject::getTitle, name);
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);
        return oneSubject;
    }

    //判断二级分类不能重复添加

    /**
     * 判断二级分类是否存在
     *
     * @param name              分类名称
     * @param parentId          分类父id
     * @return null不存在，非null存在
     */
    private EduSubject existTwoSubject(String name, String parentId) {
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<EduSubject>()
                .eq(EduSubject::getParentId, parentId)
                .eq(EduSubject::getTitle, name);
        EduSubject twoSubject = eduSubjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
